package com.caine.multithread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: CaineZhu
 * @Description:
 * @Date: Created in 17:07 2020/12/10
 * @Modified By:
 */
public class CountDownLatchDemo {
    public static void main(String[] args) {

        for (int i = 0; i < 1000; i++) {
            CountDownLatch latch2 = new CountDownLatch(1);
            CountDownLatch latch3 = new CountDownLatch(1);
            CountDownLatch latch4 = new CountDownLatch(1);
            Thread t4 = new Thread(() -> {
                try {
                    latch4.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.print(Thread.currentThread().getName());
            }, "t4");
            t4.start();
            Thread t1 = new Thread(() -> {
                System.out.print(Thread.currentThread().getName());
                latch2.countDown();
            }, "t1");
            t1.start();
            Thread t2 = new Thread(() -> {
                try {
                    latch2.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.print(Thread.currentThread().getName());
                latch3.countDown();
            }, "t2");
            t2.start();
            Thread t3 = new Thread(() -> {
                try {
                    latch3.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.print(Thread.currentThread().getName());
                latch4.countDown();
            }, "t3");
            t3.start();
            try {
                t1.join();
                t2.join();
                t3.join();
                t4.join();
                System.out.println();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


        }


    }
}
